﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace AgvDispatchSystem
{
    public class Agv
    {
        public event AgvControlChangedHandler OnAgvControlChanged;

        public AgvTrafficControlChangedHandler OnAgvTrafficControlChanged;

        #region 私有
        private byte _id;

        private byte _setcontrolByte;
        private byte _setSpeedByte;
        private byte _setTaskByte;
        private byte _setSubTaskByte;
        
        private byte _getStatusByte;
        private byte _getPathByte;
        private ushort _getRfidNum;
        private byte _getSpeedByte;
        private byte _getBatteryByte;
        private byte _getTaskByte;


        private int _driverID;
        private bool _trafficControl;

        #endregion

        #region 属性

        #region 控制属性

        /// <summary>
        /// 车号
        /// </summary>
        public byte ID
        {
            get { return _id; }
        }

        /// <summary>
        /// 控制字
        /// </summary>
        public byte SetControlByte
        {
            get { return _setcontrolByte; }
            set 
            {
                if (_setcontrolByte!=value)
                {
                    _setcontrolByte = value;
                    OnAgvControlChanged?.Invoke(this, new AgvControlChangedEventArgs(SetControlByte, SetSpeedByte, SetTaskByte, SetSubTaskByte, ID));
                }               
            }
        }

        /// <summary>
        /// 速度设置（百分比）
        /// </summary>
        public byte SetSpeedByte
        {
            get { return _setSpeedByte; }
            set
            {
                if (_setSpeedByte!=value)
                {
                    _setSpeedByte = value;
                    OnAgvControlChanged?.Invoke(this, new AgvControlChangedEventArgs(SetControlByte, SetSpeedByte, SetTaskByte, SetSubTaskByte, ID));
                }                
            }
        }

        /// <summary>
        /// 设置任务号
        /// </summary>
        public byte SetTaskByte
        {
            get { return _setTaskByte; }
            set 
            {
                if (_setTaskByte!=value)
                {
                    _setTaskByte = value;
                    OnAgvControlChanged?.Invoke(this, new AgvControlChangedEventArgs(SetControlByte, SetSpeedByte, SetTaskByte, SetSubTaskByte, ID));
                }               
            }
        }

        /// <summary>
        /// 设置系列动作中的步骤
        /// </summary>
        public byte SetSubTaskByte
        {
            get { return _setSubTaskByte; }
            set 
            {
                if (_setSubTaskByte!=value)
                {
                    _setSubTaskByte = value;
                    OnAgvControlChanged?.Invoke(this, new AgvControlChangedEventArgs(SetControlByte, SetSpeedByte, SetTaskByte, SetSubTaskByte, ID));
                }
            }
        }

        #endregion

        #region 状态属性

        /// <summary>
        /// agv状态
        /// </summary>
        public byte GetStatusByte { get => _getStatusByte; }

        /// <summary>
        /// 路线号
        /// </summary>
        public byte GetPathByte { get => _getPathByte; }

        /// <summary>
        /// rfid卡号
        /// </summary>
        public ushort GetRfidNum { get => _getRfidNum;}

        /// <summary>
        /// 当前速度
        /// </summary>
        public byte GetSpeedByte { get => _getSpeedByte;  }

        /// <summary>
        /// 电量
        /// </summary>
        public byte GetBatteryByte { get => _getBatteryByte;}

        /// <summary>
        /// 当前任务号
        /// </summary>
        public byte GetTaskByte { get => _getTaskByte;  }

        #endregion

        /// <summary>
        /// 通信超时时间
        /// </summary>
        public int TimeOut { get; set; } = 1000;
        public bool TrafficControl
        {
            get => _trafficControl;
            set
            {
                if (_trafficControl!= value)
                {
                    _trafficControl = value;
                    OnAgvTrafficControlChanged?.Invoke(this);
                }               
            }
        }

        #endregion

        public Agv(byte id,int driverID)
        {
            _id = id;
            _driverID = driverID;
    
        }

        /// <summary>
        /// 更新agv状态
        /// </summary>
        /// <param name="getStatusByte">状态字节</param>
        /// <param name="getPathByte">线路号</param>
        /// <param name="getRfidNum">rfid卡号</param>
        /// <param name="getSpeedByte">速度</param>
        /// <param name="getBatteryByte">电量</param>
        /// <param name="getTaskByte">任务号</param>
        public void UpdataStatus(byte getStatusByte,byte getPathByte,ushort getRfidNum,byte getSpeedByte,byte getBatteryByte,byte getTaskByte)
        {
            _getStatusByte = getStatusByte;
            _getPathByte = getPathByte;
            _getRfidNum = getRfidNum;
            _getSpeedByte = getSpeedByte;
            _getBatteryByte = getBatteryByte;
            _getTaskByte = getTaskByte;
        }

    }


    public class AgvControlChangedEventArgs:EventArgs
    {
        public byte SetControlByte { get; }
        public byte SetSpeedByte { get; }
        public byte SetTaskByte { get; }
        public byte SetSubTaskByte { get; }
        public byte ID { get; }

        public AgvControlChangedEventArgs(byte setControlByte,byte setSpeedByte,byte setTaskByte,byte setSubTaskByte,byte id)
        {
            SetControlByte = setControlByte;
            SetSpeedByte = setSpeedByte;
            SetTaskByte = setTaskByte;
            SetSubTaskByte = setSubTaskByte;
            ID = id;
        }
    }

    /// <summary>
    /// agv控制改变事件
    /// </summary>
    /// <param name="sender">源</param>
    /// <param name="e">事件信息</param>
    public delegate void AgvControlChangedHandler(object sender, AgvControlChangedEventArgs e);

    public delegate void AgvTrafficControlChangedHandler(object sender);
}
